package com.android.home.fragment;

import android.view.ViewGroup;
import com.android.common.mvp.model.CodeMsgBean;
import com.android.common.base.rv.BaseRvAdapter;
import com.android.common.base.rv.BaseRvFragment;
import com.android.common.base.rv.BaseRvHolder;
import com.android.common.utils.Router;
import com.android.home.BuildConfig;
import com.android.home.R;
import com.android.home.adapter.holder.GroupContentHolder;
import com.android.home.adapter.holder.GroupTitleHolder;
import com.android.home.mvp.model.home.GroupBean;
import com.android.home.mvp.present.home.HomePresent;

public class VPGroupFragment extends BaseRvFragment<GroupBean.ItemsBean> {

    public static HomeSecondFragment instance;

    private HomePresent present;

    private boolean mNeedBack = false;

    public VPGroupFragment() {
    }

    public static HomeSecondFragment getInstance() {
        if (null == instance) {
            instance = new HomeSecondFragment();
        }

        return instance;
    }

    @Override
    protected void onInitView() {
        super.onInitView();
        present = new HomePresent(this);
    }

    @Override
    protected boolean onNeedBack() {
        return mNeedBack;
    }

    @Override
    protected boolean onNeedToolBar() {
        return false;
    }

    @Override
    protected BaseRvAdapter<GroupBean.ItemsBean> initAdapter() {
        BaseRvAdapter<GroupBean.ItemsBean> adapter = new BaseRvAdapter<GroupBean.ItemsBean>(this.getContext()) {
            @Override
            protected BaseRvHolder<GroupBean.ItemsBean> createDataViewHolder(ViewGroup parent, int viewType) {
                return new GroupTitleHolder(mLayoutInflater.inflate(R.layout.item_group, parent, false)) {
                    @Override
                    protected BaseRvAdapter<GroupBean.ItemsBean.ResultBean> initAdapter(GroupTitleHolder holder) {
                        return createGroupContent(holder);
                    }
                };
            }
        };
        adapter.setOnItemClickListener((view, position) -> {
            Router.getInstance().startActivity(this.getContext(), null, BuildConfig.HOME_SECOND);
        });
        return adapter;
    }

    private BaseRvAdapter<GroupBean.ItemsBean.ResultBean> createGroupContent(GroupTitleHolder holder){
        BaseRvAdapter<GroupBean.ItemsBean.ResultBean> adapter = new BaseRvAdapter<GroupBean.ItemsBean.ResultBean>(this.getContext()) {
            @Override
            protected BaseRvHolder<GroupBean.ItemsBean.ResultBean> createDataViewHolder(ViewGroup parent, int viewType) {
                return new GroupContentHolder(mLayoutInflater.inflate(R.layout.item_home_second, parent, false));
        };

    };
        return adapter;
    }

    @Override
    public void onFailed(CodeMsgBean responseBean, String methodName) {
        super.onFailed(responseBean, methodName);
        refreshOrLoadFailed();
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        present.getGroupList();
    }

    @Override
    public void onPullRefresh() {
        super.onPullRefresh();
        present.getGroupList();
    }

    @Override
    public void onPullLoadMore() {
        super.onPullLoadMore();
        present.getGroupList();
    }

    @Override
    public<D> void onSucceed(D d, String methodName) {
        super.onSucceed(d, methodName);
        if (d instanceof GroupBean){
            GroupBean groupBean = (GroupBean)d;
            refreshOrLoadSuccess(groupBean.getItems());
        }
    }


    public void setNeedBack(boolean needBack) {
        this.mNeedBack = needBack;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        instance = null;
    }
}
